﻿Imports System.IO
Imports System.Diagnostics

Public Class frmMain

    Private WithEvents oApp As SMSClasses.App
    Private WithEvents oContacts As SMSClasses.People
    Private WithEvents oSMS As SMSClasses.SMSMessages

    Private SelPerson As SMSClasses.Person
    Private SelRow As Integer
    Private BalloonEnabled As Boolean = True

    '********************************************************************
    'To Do
    '********************************************************************
    'Need to allow deleting of individual messages
    'Need to allow deleting of threads
    '
    '********************************************************************

    Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        oApp.Save()
        Me.NotifyIcon1.Visible = False
        oApp = Nothing
        SelPerson = Nothing

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Initialise()

    End Sub

    Private Sub Initialise()

        Dim inPath As String = My.Settings.in_box

        If inPath <> "" Then
            inbox_watcher.Path = My.Settings.in_box
            inbox_watcher.EnableRaisingEvents = True
            oApp = SMSClasses.App.Instance
            oContacts = oApp.Contacts
            Me.bsContacts.DataSource = oContacts
        Else
            ShowSettings()
        End If

    End Sub

    Private Sub AddContactToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AddContactToolStripMenuItem.Click

        Dim frmAdd As New Add_Contact()
        frmAdd.ShowDialog()

    End Sub

    Private Sub LoadSMS(P As SMSClasses.Person)

        If P Is Nothing Then Return

        Me.dgSMS.Rows.Clear()

        Dim s As SMSClasses.SMSMessage
        For Each s In P.SMSMessages
            Dim dgRow As New DataGridViewRow
            Dim dcImage As New DataGridViewImageCell
            Dim dcText As New DataGridViewTextBoxCell

            dcText.Value = s.DisplayText
            dcText.Style = s.Style
            dgRow.Cells.Add(dcImage)
            dgRow.Cells.Add(dcText)
            Me.dgSMS.Rows.Add(dgRow)

        Next


    End Sub

    Private Sub inbox_watcher_Created(sender As Object, e As System.IO.FileSystemEventArgs) Handles inbox_watcher.Created

        ProcessMessage(e.FullPath)

    End Sub

    Private Sub ProcessMessage(Path As String)

        Dim strContent As String = SMSClasses.Utility.FileToText(Path)

        If strContent <> "" Then
            oApp.ProcessIncomingMessage(strContent)
            LoadSMS(SelPerson)
        End If

    End Sub


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        If SelPerson Is Nothing Then
            MsgBox("You must select a recipient", vbOKOnly, vbInformation)
            Return
        End If

        Dim SMS As New SMSClasses.SMSMessage
        SMS.FromName = "NULL_VALUE"
        SMS.FromPhone = "5555555555"
        SMS.ToName = SelPerson.Name
        SMS.ToPhone = SelPerson.Telephone
        SMS.Body = Me.txtSMS.Text
        SMS.Read = True
        SMS.Type = 1
        SMS.Status = 3
        Me.txtSMS.Text = ""

        'Also need to send a message for the tablet to pick up
        oApp.SendSMSMessage(SMS, My.Settings.out_box, My.Settings.in_box)

    End Sub

    Private Sub Form2ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles Form2ToolStripMenuItem.Click

        ShowSettings()

    End Sub

    Private Sub ShowSettings()

        Dim fSet As New frmSettings
        fSet.ShowDialog()

    End Sub


    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Me.Close()

    End Sub

    Private Sub ImportToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ImportToolStripMenuItem.Click

        Dim fb As New OpenFileDialog
        Dim fPath As String = ""
        With fb
            .Multiselect = False
            If .ShowDialog = DialogResult.OK Then
                fPath = .FileName
            End If
        End With

        If fPath = "" Then Return
        BalloonEnabled = False
        oApp.Import(fPath)
        BalloonEnabled = True

    End Sub


    Private Sub ContactsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ContactsToolStripMenuItem.Click

        Dim frmCon As New Contacts
        frmCon.ShowDialog()

    End Sub

    Private Sub dgContacts_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgContacts.CellContentClick

        Dim sName As String

        sName = dgContacts.Item(0, dgContacts.CurrentRow.Index).Value

        Dim p As SMSClasses.Person
        For Each p In oApp.Contacts
            If p.Name = sName Then
                SelPerson = p
                oSMS = SelPerson.SMSMessages
                Me.bsSMS.DataSource = oSMS

                Exit For
            End If
        Next

    End Sub

    Private Sub oSMS_ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs) Handles oSMS.ListChanged

        Dim b As Boolean
        b = oSMS.HasUnReadMessages

        If b = False Then
            Dim fnt = New Font("Calibri", 10, FontStyle.Regular)
            Dim d As New DataGridViewCellStyle
            d.Font = fnt
            dgContacts.Item(0, dgContacts.CurrentRow.Index).Style = d
            dgContacts.Item(1, dgContacts.CurrentRow.Index).Style = d

        End If


    End Sub

    Private Sub dgSMS_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgSMS.CellFormatting

        If e.RowIndex >= 0 Then

            If oSMS.Item(e.RowIndex).FromName = "NULL_VALUE" Then
                e.CellStyle.BackColor = Color.Beige
            End If

            e.CellStyle.Font = oSMS.Item(e.RowIndex).Font
        End If

    End Sub

    Private Sub dgContacts_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgContacts.CellFormatting

        If e.RowIndex >= 0 Then

            If oContacts.Item(e.RowIndex).HasUnReadMessages = True Then
                Dim fnt As Font
                fnt = New Font("Calibri", 10, FontStyle.Bold)
                e.CellStyle.Font = fnt
            End If

        End If

    End Sub

    Private Sub dgSMS_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgSMS.CellContentClick

        If e.RowIndex >= 0 Then
            SelPerson.SMSMessages(e.RowIndex).Read = True
        End If

    End Sub

    Private Sub dgContacts_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgContacts.MouseClick

        If e.Button = Windows.Forms.MouseButtons.Right Then

            Me.cMenuContacts.Items.Clear()
            Me.cMenuContacts.Items.Add("Mark All As Read")
            Me.cMenuContacts.Items.Add("Delete Thread")
            Me.cMenuContacts.Show()

        End If

    End Sub

    Private Sub cMenuContacts_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles cMenuContacts.ItemClicked

        'e.ClickedItem.Text


    End Sub

    Private Sub dgSMS_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgSMS.MouseClick

        If e.Button = Windows.Forms.MouseButtons.Right Then

            Me.cMenuContacts.Items.Clear()
            Me.cMenuContacts.Items.Add("Delete Item")
            Me.cMenuContacts.Show()

        End If

    End Sub
End Class
